﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace CostaRicaPureTravelUtils
{
    public class MailService
    {
        public static void SendAsync(string address, string displayName, string toAddress, string toDisplayName,
            MailPriority priority, string subject, string body, bool isHTML, string host, int port, bool enableSsl,
            bool defaultCredentials = true, string username = null, string password = null)
        {
            MailMessage msg = createMessage(address, displayName, toAddress, toDisplayName, priority, subject, body, isHTML);
            SmtpClient client = createClient(host, port, enableSsl, defaultCredentials, username, password);
            client.SendCompleted += (s, e) =>
            {
                client.Dispose();
                msg.Dispose();
            };
            client.SendAsync(msg, null);
        }

        public static void Send(string address, string displayName, string toAddress, string toDisplayName,
            MailPriority priority, string subject, string body, bool isHTML, string host, int port, bool enableSsl,
            bool defaultCredentials = true, string username = null, string password = null)
        {
            SmtpClient client = null;
            MailMessage msg = null;
            try
            {
                msg = createMessage(address, displayName, toAddress, toDisplayName, priority, subject, body, isHTML);
                client = createClient(host, port, enableSsl, defaultCredentials, username, password);
                client.Send(msg);
            }
            finally
            {
                if (client != null) client.Dispose();
                if (msg != null) msg.Dispose();
            }
        }

        private static MailMessage createMessage(string address, string displayName, string toAddress, string toDisplayName, 
            MailPriority priority, string subject, string body, bool isHTML)
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(address, displayName);
            // To addresses
            msg.To.Add(toAddress);
            msg.To.Add(new MailAddress(toAddress, toDisplayName));
            msg.Priority = MailPriority.High;
            msg.Subject = subject;
            msg.Body = body;
            msg.IsBodyHtml = true;

            return msg;
        }

        private static SmtpClient createClient(string host, int port, bool enableSsl,
            bool defaultCredentials = true, string username = null, string password = null)
        {
            SmtpClient client = new SmtpClient();
            client.Host = host;
            client.Port = port;
            client.EnableSsl = enableSsl;
            client.ServicePoint.MaxIdleTime = 1; 

            client.UseDefaultCredentials = defaultCredentials;
            if (!defaultCredentials)
            {
                // Provide your credentials
                client.Credentials =
                    new System.Net.NetworkCredential(username, password);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
            }

            return client;
        }
    }
}
